Page({
    data: {
        currentCategory: 'all', // 当前选中的分类：all/system/reservation/health
        // 分类配置数组
        categories: [
            { name: '全部消息', key: 'all' },
            { name: '系统消息', key: 'system' },
            { name: '预约消息', key: 'reservation' },
            { name: '健康提醒', key: 'health' }
        ],
        messages: [],           // 所有消息数据
        filteredMessages: [],   // 筛选后的消息
        loading: true           // 加载状态
    },

    onLoad: function () {
        this.fetchMessages(); // 加载消息数据
    },

    /**
     * 模拟从服务器获取消息数据
     */
    fetchMessages: function () {
        this.setData({ loading: true });

        // 模拟网络请求延迟
        setTimeout(() => {
            const mockMessages = [
                {
                    id: 1,
                    category: 'system',
                    title: '系统通知',
                    desc: '您的账号已成功绑定手机号',
                    time: '2025-11-20 10:15',
                    unread: true,
                    image:"/assets/images/message/系统.png"
                },
                {
                    id: 2,
                    category: 'reservation',
                    title: '预约提醒',
                    desc: '您预约的洁牙服务将于明天上午10点开始',
                    time: '2025-11-20 09:30',
                    unread: true,
                    image:"/assets/images/message/预约信息.png"
                },
                {
                    id: 3,
                    category: 'health',
                    title: '健康提醒',
                    desc: '今天是您的定期检查日，记得按时前往',
                    time: '2025-11-19 16:40',
                    unread: false,
                    image:"/assets/images/message/健康.png"
                },
                {
                    id: 4,
                    category: 'reservation',
                    title: '预约成功',
                    desc: '您已成功预约牙齿美白服务，订单号：DENT20251102002',
                    time: '2025-11-19 15:20',
                    unread: false,
                    image:"/assets/images/message/预约信息.png"
                },
                {
                    id: 5,
                    category: 'system',
                    title: '版本更新',
                    desc: '小程序已更新至最新版本，新增多项功能',
                    time: '2025-11-18 14:00',
                    unread: false,
                    image:"/assets/images/message/系统.png"
                },
                {
                    id: 6,
                    category: 'health',
                    title: '口腔护理提醒',
                    desc: '冬季干燥，记得多喝水并使用温和的口腔护理产品',
                    time: '2025-11-18 10:00',
                    unread: true,
                    image:"/assets/images/message/健康.png"
                }
            ];

            this.setData({
                messages: mockMessages,
                loading: false
            }, () => {
                this.filterMessages(); // 初始化筛选消息
            });
        }, 1000);
    },

    /**
     * 切换消息分类
     */
    switchCategory: function (e) {
        const category = e.currentTarget.dataset.category;
        this.setData({
            currentCategory: category
        }, () => {
            this.filterMessages(); // 切换分类后重新筛选
        });
    },

    /**
     * 根据当前分类筛选消息
     */
    filterMessages: function () {
        const { currentCategory, messages } = this.data;
        let filtered = [];

        if (currentCategory === 'all') {
            filtered = messages; // 全部消息
        } else {
            filtered = messages.filter(item => item.category === currentCategory);
        }

        this.setData({
            filteredMessages: filtered
        });
    },

    /**
     * 获取分类对应的图标
     */

    //  这部分没有改好啊啊啊
    // getCategoryIcon: function (category) {
    //     switch (category) {
    //         case 'system':
    //             return '/assets/images/message/系统.png';
    //         case 'reservation':
    //             return '../../../assets/images/message/预约信息.png';
    //         case 'health':
    //             return '../../../assets/images/message/健康.png';
    //         default:
    //             return '../../../assets/images/message/消息.png';
    //     }
    // },

    /**
     * 获取分类对应的名称
     */
    getCategoryName: function (category) {
        switch (category) {
            case 'system':
                return '系统';
            case 'reservation':
                return '预约';
            case 'health':
                return '健康';
            default:
                return '';
        }
    },

    /**
     * 获取分类标签的样式类
     */
    getCategoryTagClass: function (category) {
        return `tag-${category}`;
    },

    /**
     * 获取空状态提示文本
     */
    getEmptyText: function () {
        const { currentCategory } = this.data;
        let categoryName = '相关';

        if (currentCategory === 'system') {
            categoryName = '系统';
        } else if (currentCategory === 'reservation') {
            categoryName = '预约';
        } else if (currentCategory === 'health') {
            categoryName = '健康';
        }

        return `暂无${categoryName}消息`;
    },

    /**
     * 查看消息详情
     */
    viewMessage: function (e) {
        const id = e.currentTarget.dataset.id;

        // 更新消息为已读状态
        const updatedMessages = this.data.messages.map(item => {
            if (item.id === id) {
                return { ...item, unread: false };
            }
            return item;
        });

        this.setData({
            messages: updatedMessages
        }, () => {
            this.filterMessages(); // 重新筛选以更新界面

            // 跳转到详情页
            wx.navigateTo({
                url: `/pages/message-detail/message-detail?id=${id}`
            });
        });
    },

    /**
     * 下拉刷新
     */
    onPullDownRefresh: function () {
        this.fetchMessages();
        wx.stopPullDownRefresh();
    }
});